package com.jsmtr.rms.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DBHelperUtil {

    public DBHelperUtil() {
    }

    /**
     * 根据实体类名称得到表名
     *
     * @param clss
     * @return 表名
     */
    @SuppressWarnings("rawtypes")
    public static String getTableNameByClass(Class clss) {
        String tableName = "";
        String className = clss.getSimpleName();
        StringBuffer clssBuffer = new StringBuffer();
        for (int i = 0; i < className.length(); i++) {
            char c = className.charAt(i);
            if (Character.isLowerCase(c)) {
                clssBuffer.append(c);
            } else {
                clssBuffer.append("_" + Character.toLowerCase(c));
            }
        }
        tableName = "tb" + clssBuffer.toString();
        return tableName;
    }

    /**
     * 根据实体类名称得到属性名和表名的MAP
     *
     * @param clss
     * @return MAP    属性名和表名的MAP<属性名,对应表的列名>
     */
    @SuppressWarnings("rawtypes")
    public static Map<String, String> getTableClumns(Class clss) {
        Map<String, String> clumns = new HashMap<String, String>();
        Field[] fields = clss.getDeclaredFields();
        String fieldName = "";
        for (int i = 0; i < fields.length; i++) {
            StringBuffer fnbBuffer = new StringBuffer();
            fieldName = fields[i].getName();
            for (int j = 0; j < fieldName.length(); j++) {
                char c = fieldName.charAt(j);
                if (Character.isLowerCase(c)) {
                    fnbBuffer.append(c);
                } else {
                    fnbBuffer.append("_" + Character.toLowerCase(c));
                }
            }
            if (!fieldName.equalsIgnoreCase("serialVersionUID")) {
                clumns.put(fieldName, fnbBuffer.toString());
            }


        }
        return clumns;

    }

    /**
     * 根据属性名称得到列名
     *
     * @param fieldName
     * @return 数据库中的列名
     */
    public static String getTableClumnName(String fieldName) {
        StringBuffer fnbBuffer = new StringBuffer();
        for (int j = 0; j < fieldName.length(); j++) {
            char c = fieldName.charAt(j);
            if (Character.isLowerCase(c)) {
                fnbBuffer.append(c);
            } else {
                fnbBuffer.append("_" + Character.toLowerCase(c));
            }
        }
        return fnbBuffer.toString();
    }

    /**
     * 填充实体类
     *
     * @param dt   要填充的
     * @param clss
     * @return
     * @throws NoSuchFieldException
     * @throws SecurityException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    @SuppressWarnings({"rawtypes", "unchecked", "null"})
    public static List fillDataInfo(Map<String, String> dt, Class clss) throws NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException {
        List re = null;
        Map<String, String> clumns = dt;
        Object temp = clss.newInstance();
        for (String fieldName : clumns.keySet()) {
            Field field = clss.getField(fieldName);
            field.set(re, dt.get(clumns.get(fieldName).toUpperCase()));

        }
        re.add(temp);
        return re;
    }


}
